package com.baselibs.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.baselibs.utils.DensityUtils;

/**
 * 作者：xudiwei
 * <p/>
 * 日期： Administrator on 2015/8/25.
 * <p/>
 * 文本描述：字母导航View
 */
public class LetterBar extends View {

    private String[] lets = {"A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
            "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"};
    private Paint mPaint;

    /**
     * 当前控件的宽度
     */
    private int width;
    /**
     * 当前控件的高度
     */
    private int height;
    /**
     * 当前选中字母的索引
     */
    private int choosePoint;

    /**
     * 文本的高度
     */
    private int textHeight;

    /**
     * 默认的背影色
     */
    private final static String BACKCOLOR_DEFAULT = "#00000000";

    /**
     * 触摸时的背影色
     */
    private final static String BACKCOLOR_TOUCH = "#33000000";

    /**
     * 大文本显示
     */
    private TextView textView;

    private onLetterSelectedListener listener;

    public LetterBar(Context context) {
        super(context);
    }

    public LetterBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setBackgroundColor(Color.parseColor(BACKCOLOR_DEFAULT));
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.parseColor("#ff67c729"));
        mPaint.setTypeface(Typeface.DEFAULT);
        mPaint.setTextSize(DensityUtils.dipToPx(getContext(),15));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        width = this.getMeasuredWidth();
        height = this.getMeasuredHeight();
        //文本的宽,可以通过Paint的方法进行测量获得
        float textWidth = mPaint.measureText(lets[0]);
        //文本的高,是当前控件的高度除以字母数量的长度
        textHeight = height / lets.length;
        //文本的X轴坐标,是当前控件的宽度减去文本的宽度再除以2
        int poX = (int) ((width - textWidth) / 2);
        for (int i = 0; i < lets.length; i++) {
            if (i == choosePoint) {
                mPaint.setColor(Color.RED);
            } else {
                mPaint.setColor(Color.parseColor("#ff67c729"));
            }
            canvas.drawText(lets[i], poX, textHeight * (i + 1), mPaint);
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                setBackgroundColor(Color.parseColor(BACKCOLOR_DEFAULT));
                //当手指松开时应该把选中字母索引的变量设置-1表示不选中任何一个
                choosePoint = -1;
                if (null != textView) {
                    textView.setVisibility(View.INVISIBLE);
                }
                invalidate();
                break;
            default:
                choosePoint = y / textHeight;
                setBackgroundColor(Color.parseColor(BACKCOLOR_TOUCH));
                if (null != textView) {
                    if (choosePoint >= 0 && choosePoint < lets.length) {
                        //根据计算出来的索引获取字字母
                        String let = lets[choosePoint];
                        textView.setText(let);
                        textView.setVisibility(View.VISIBLE);
                        //回调
                        if (null != listener){
                            listener.onLetterSelected(let);
                            System.out.println(listener);
                        }
                    }
                }

                invalidate();
        }

        return true;
    }

    /**
     * 大文本显示
     * @param textView
     */
    public void setTextView(TextView textView) {
        this.textView = textView;
    }

    /**
     * 设置字母选中回调方法
     * @param listener
     */
    public void setOnLetterSelectedListener(onLetterSelectedListener listener){
        this.listener = listener;
    }

    /**
     * 回调接口
     */
    public interface onLetterSelectedListener{
        void onLetterSelected(String letter);
    }
}
